package com.meevle.utilities;

import org.red5.core.ApplicationSettings;

/**
 * Checks if the parameters mathch a certain regex. If not, they throw an IllegalArgumentException. Note that the returned values
 *  in the Exception messagees should be stripped from harmful code.
 * @author Jeroen Ingelbrecht
 *
 */
public class RegexController {

	private volatile static RegexController instance = null;
	
	private RegexController()
	{}
	
	public static RegexController getInstance () {
        if (instance == null)
        {
        	synchronized (RegexController.class)
        	{
        		if (instance == null) instance = new RegexController ();
			}
        }
        return instance;
    }
	
	/**
	 * Checks if the username doesn't contain any illegal characters
	 * @param username
	 * @throws IllegalArgumentException if the username contains any illegal characters. It doesn't say what characters though.
	 */
	public void checkUsername(String username)throws IllegalArgumentException, NullPointerException
	{
		if(username==null)
			throw new NullPointerException("Username is NULL!!!");
		if(!username.matches(ApplicationSettings.getInstance().getRegexUser() ) )
		{
			username = ProcessBBcodeJava.getInstance().preparePostText(username);
			throw new IllegalArgumentException("The username " + username+ " contains one or more illegal characters");
		}
	}
	
	/**
	 * Checks if the trackname doesn't contain any illegal characters
	 * @param trackname
	 * @throws IllegalArgumentException if the trackname contains any illegal characters. It doesn't say what charachers though.
	 */
	public void checkSlotname(String trackname)throws IllegalArgumentException
	{
		if(!trackname.matches(ApplicationSettings.getInstance().getRegexSlot() ) )
		{
			trackname = ProcessBBcodeJava.getInstance().preparePostText(trackname);
			throw new IllegalArgumentException("The trackname " +trackname+ " contains one or more illegal characters");
		}
	}
	
	/**
	 * Checks if the projectname doesn't contain any illegal characters
	 * @param projectname
	 * @throws IllegalArgumentException if the projectname contains any illegal characters. It doesn't say what charachers though.
	 */
	public void checkSetname(String projectname)throws IllegalArgumentException
	{
		if(!projectname.matches(ApplicationSettings.getInstance().getRegexSet() ) )
		{
			projectname = ProcessBBcodeJava.getInstance().preparePostText(projectname);
			throw new IllegalArgumentException("The projectname "+projectname+" contains one or more illegal characters");
		}
	}
	
	/**
	 * Checks if the password doesn't contain any illegal characters
	 * @param password
	 * @throws IllegalArgumentException if the password contains any illegal characters. It doesn't say what charachers though.
	 */
	public void checkPassword(String password)throws IllegalArgumentException
	{
		if(!password.matches(ApplicationSettings.getInstance().getRegexPassword() ) )
		{
			password = ProcessBBcodeJava.getInstance().preparePostText(password);
			throw new IllegalArgumentException("The password "+password+" contains one or more illegal characters");
		}
	}
	
	/**
	 * Checks if the track comment's rating doesn't contain any illegal characters
	 * @param rating
	 * @throws IllegalArgumentException if the rating contains any illegal characters. It doesn't say what charachers though.
	 */
	public void checkRatingSlotComment(String rating)throws IllegalArgumentException
	{
		if(!rating.matches(ApplicationSettings.getInstance().getRegexRatingSlotComment() ) )
		{
			rating = ProcessBBcodeJava.getInstance().preparePostText(rating);
			throw new IllegalArgumentException("The rating "+rating+" contains one or more illegal characters");
		}
	}
	
}
